package org.xxpay.manage.order.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xxpay.core.common.constant.Constant;
import org.xxpay.core.common.constant.RetEnum;
import org.xxpay.core.common.domain.BizResponse;
import org.xxpay.core.common.domain.XxPayPageRes;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayOrderLog;
import org.xxpay.manage.common.ctrl.BaseController;
import org.xxpay.manage.common.service.CommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/pay_order_log")
public class PayOrderLogController extends BaseController {

    @Autowired
    private static final MyLog _log = MyLog.getLog(PayOrderLogController.class);

    /**
     * 订单详情查询
     * @return
     */
    @RequestMapping("/select")
    @ResponseBody
    public ResponseEntity<?> select(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        String orderType = getString(param, "orderType"); // 订单号类型
        String orderId = getString(param, "orderId"); // 订单号

        if (StringUtils.isEmpty(orderId) || StringUtils.isEmpty(orderType)) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_PAY_ORDER_LOG_ORDERID_NULL));
        }
        PayOrderLog payOrderLog = new PayOrderLog();
        if ("1".equals(orderType)) {
            payOrderLog.setPayOrderId(orderId);
        }else if ("2".equals(orderType)) {
            payOrderLog.setMchOrderNo(orderId);
        }else {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_STATUS_ERROR));
        }

        List<PayOrderLog> payOrderLogList = commonService.payOrderLogService.select(payOrderLog);
        if (payOrderLogList.size() == 0) return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_RECORD_NOT_EXIST));
        return ResponseEntity.ok(XxPayPageRes.buildSuccess(payOrderLogList));
    }


}